package cn.jsc.org.controller;

import cn.jsc.basic.util.AjaxResult;
import cn.jsc.basic.util.PageList;
import cn.jsc.org.domain.Department;
import cn.jsc.org.query.DepartmentQuery;
import cn.jsc.org.service.IDepartmentServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
   /* @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")  //删除的
    @PostMapping  //一般做分页查询的
    @PatchMapping //一般用来做批量操作的
    @PutMapping  //一般来做新增或修改
    @GetMapping  //获取，查询的

    @RequestBody :前台请求体中的json数据会帮你转成对象

    public void deleteById(@PathVariable Long id){
        //调用删除
        departmentService.delete(id);
    }

    @RequestMapping
    public void findAll(){
        //调用删除

    }

    swagger测试：
    http://localhost:8080/swagger-ui.html
*/

@RestController//@ResponseBody+@Controller
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")

public class DepartmentController {

    @Autowired
    private IDepartmentServiceI departmentService;

    //删除  根据id删除  localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @PatchMapping//批量删除操作
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询一个对象 根据id  localhost:8080/dept/1
    @GetMapping("/{id}")
    public Department queryById(@PathVariable Long id){
        return departmentService.getById(id);
    }

    /**
     * /新增或修改  根据id来区分
     *  @RequestBody :前台请求体中的json数据会帮你转成对象
     *  localhost:8080/dept
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询所有的对象 localhost:8080/dept
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.getAll();
    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }

    //查询分页数据 localhost:8080/dept
    @PostMapping
    public PageList<Department> queryData(@RequestBody  DepartmentQuery query){
        return departmentService.queryData(query);
    }
}
